<?php
namespace app\service;
use EasyWeChat\Foundation\Application;
use think\Db;

class WechatServie
{
    public $appid = '';
    public $secret = '';
 
    public function __construct(){
        $this->appid = getenv('WECHAT_GZH_APPID');
        $this->secret = getenv('WECHAT_GZH_SECRET');
    }

    public function app()
    {
        $options = [
            'app_id' => $this->appid,
            'secret' => $this->secret
        ];

        $app = new Application($options);
        return $app;
    }

    public function getAccessToken()
    {
        $app = (new WechatServie)->app();

        // 获取 access token 实例
        $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
        $token = $accessToken->getToken(); // token 字符串
        // $token = $accessToken->getToken(true); // 强制重新从微信服务器获取 token.
        return $token;
    }

    public static function getUserUnionid($openid)
    {
        $access_token = (new WechatServie)->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
        $data = curl($url, "");
        if($data && $data['code'] == 200){
            $data = json_decode($data['data']);
            if($data && $data->unionid){
                return $data->unionid;
            }
        }
        return false;
    }


    public static function bindUnionid($openid)
    {
        $unionid = self::getUserUnionid($openid);
        if(!$unionid){
            return false;
        }
        $WechatUnion = Db::name('wechat_union')->where(['unionid'=>$unionid])->find();
        if($WechatUnion){
            if($WechatUnion['wxgzh_openid']){
                return $WechatUnion['user_id'];
            }
            $update = Db::name('wechat_union')->where(['user_id'=>$WechatUnion['user_id']])->update(['wxgzh_openid'=>$openid]);
            if($update){
                return $WechatUnion['user_id'];
            }
        }
        return false;
    }
    // 公众号菜单处理
    public static function GzhMenu()
    {
        $app = (new WechatServie)->app();

        $menu = $app->menu;

        $menus = $menu->current();


        return json_decode($menus, true);
    }

    public static function GzhMenuAdd()
    {
        $app = (new WechatServie)->app();

        $menu = $app->menu;

        $buttons = [
            [
                "type" => "click",
                "name" => "绑定账号",
                "key"  => "BIND"
            ],
            [
                "type" => "view",
                "name" => "下载APP",
                "url"  => "http://zaima.meiyue.co/"
            ],
            // [
            //     "name"       => "菜单",
            //     "sub_button" => [
            //         [
            //             "type" => "view",
            //             "name" => "搜索",
            //             "url"  => "http://www.soso.com/"
            //         ],
            //         [
            //             "type" => "view",
            //             "name" => "视频",
            //             "url"  => "http://v.qq.com/"
            //         ],
            //         [
            //             "type" => "click",
            //             "name" => "赞一下我们",
            //             "key" => "V1001_GOOD"
            //         ],
            //     ],
            // ],
        ];
        $res = $menu->add($buttons);
        return json_decode($res, true);
    }

    public static function GzhMenuDel($menuId = '')
    {
        $app = (new WechatServie)->app();

        $menu = $app->menu;

        if($menuId){
            $menu->destroy($menuId);
        }else{
            $menu->destroy(); // 全部
        }
    }
}